package com.yanqu.road.server.cmd.player;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eMessageType;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.entername.EnterNameActivityConfig;
import com.yanqu.road.server.manger.activity.entername.EnterNameActivityMgr;
import com.yanqu.road.server.manger.activity.entername.EnterNameCache;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.server.manger.activity.TradeWarActivityMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWarActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.Protocol;
import io.netty.channel.ChannelHandlerContext;


@Cmd(code = Protocol.S_WORLD_MESSAGE_TRANSFER, desc = "世界聊天转发")
public class ChatTransferCmd implements Command {


    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {
        boolean groupActivityType = false;
        WorldMessageProto.WorldMessageTempMsg chatMsg = WorldMessageProto.WorldMessageTempMsg.parseFrom(yanQuMessage.getBytes());
        int groupId = chatMsg.getGroupId();
        int activityId = chatMsg.getActivityId();
        if (activityId > 0 && groupId > 0) {
            ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
            if(activityInfo == null){
                activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
            }
            if(activityInfo == null){
                activityInfo = StrongholdActivityMgr.getOpenActivityInfo(activityId);
            }
            if (activityInfo == null) {
                activityInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
            }
            if (activityInfo == null) {
                activityInfo = TradeWarActivityMgr.getOpenActivityInfo(activityId);
            }
            if (activityInfo != null) {
                int type = activityInfo.getType();
                groupActivityType = UnionActivityGroupHelper.isGroupActivityType(type);
            }
        }


        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            //是否是地域报名活动,分组聊天
            if(activityId > 0 && chatMsg.getType() == eMessageType.EnterNameActivityGroupChat.getValue() && chatMsg.hasGroupId() && chatMsg.getGroupId() > 0){
                EnterNameActivityConfig config = EnterNameActivityMgr.getActivityConfig(activityId);
                if(config != null){
                    EnterNameCache enterNameCache = EnterNameActivityMgr.getEnterNameCache(activityId, player.getUserId());
                    if(enterNameCache != null){
                        if(chatMsg.getGroupId() == EnterNameActivityMgr.getGroupId(activityId, enterNameCache.getZoneId())){
                            player.sendPacket(Protocol.U_WORLD_MESSAGE_SYNC, WorldMessageProto.WorldMessageTempMsg.parseFrom(yanQuMessage.getBytes()).toBuilder());
                        }
                    }
                }
                continue;
            }
            if (groupActivityType) {
                if (GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId()) == groupId) {
                    player.sendPacket(Protocol.U_WORLD_MESSAGE_SYNC, WorldMessageProto.WorldMessageTempMsg.parseFrom(yanQuMessage.getBytes()).toBuilder());
                }
            }else {
                player.sendPacket(Protocol.U_WORLD_MESSAGE_SYNC, WorldMessageProto.WorldMessageTempMsg.parseFrom(yanQuMessage.getBytes()).toBuilder());
            }
        }
    }
}
